package de.idgard.android.addon.ui.dialogs;

import android.app.Dialog;
import android.content.Context;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.RadioGroup;
import de.idgard.android.R;
import de.idgard.android.addon.services.GlobalServiceRegistry;
import de.idgard.android.addon.ui.AddonUIManager;
import de.idgard.android.addon.ui.services.TransparencyStateService;
import de.idgard.android.addon.ui.services.TransparencyStateService.TransparencyState;
import de.idgard.json.response.TransparencyChangeResponse;
import de.idgard.json.transport.Receiver;

/**
 * 
 * @author ckaratza Transparency Dialog adjusted to resolve changes in
 *         Transparency Mode even if locale is not the default. Prior comparison
 *         would have failed for other locales.
 * 
 * @author Pratik - Button's ID Comparison for Transparency level setting.
 *         customize dialog by adding custom title,button,background.
 * 
 */

public class TransparencyDialog extends Dialog {

	private TransparencyStateService getTransparencyStateService() {
		return GlobalServiceRegistry.instance().getTransparencyStateService();
	}

	public TransparencyDialog(Context context) {
		super(context);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.idgard_dialog_transparency);
		getWindow().setWindowAnimations(android.R.anim.cycle_interpolator);
		buildGUI();
	}

	private void buildGUI() {
		RadioGroup tradioGroup = (RadioGroup) findViewById(R.id.transparencyRadioGroup);
		Button tCancel = (Button) findViewById(R.id.tCancel);

		AddonUIManager.getInstance().updateIDGardGUI();

		tCancel.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				dismiss();
			}
		});

		if (getTransparencyStateService().isIncognito()) {
			tradioGroup.check(R.id.tIncognito);
		} else if (getTransparencyStateService().isTransparent()) {
			tradioGroup.check(R.id.tOpen);
		} else {
			tradioGroup.check(R.id.tNormal);
		}

		tradioGroup
				.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {

					@Override
					public void onCheckedChanged(RadioGroup group, int checkedId) {

						group.check(checkedId);
						TransparencyStateService.TransparencyState level;

						if (checkedId == R.id.tOpen) {
							level = TransparencyStateService.TransparencyState.TRANSPARENT;
						} else if (checkedId == R.id.tNormal) {
							level = TransparencyStateService.TransparencyState.NORMAL;
						} else if (checkedId == R.id.tIncognito) {
							level = TransparencyStateService.TransparencyState.INCOGNITO;
						} else {
							level = TransparencyStateService.TransparencyState.NORMAL;
						}
						changeTransparencyLevel(level);
					}
				});
	}

	public void changeTransparencyLevel(final TransparencyState level) {

		GlobalServiceRegistry.instance().getBackendService()
				.changeTransparencyLevel(level.value())
				.fire(new Receiver<TransparencyChangeResponse>() {
					@Override
					public void onSuccess(TransparencyChangeResponse response) {
						GlobalServiceRegistry.instance()
								.getOrchestrationService()
								.adjustForTransparencyLevel(level);
						AddonUIManager.getInstance().updateIDGardGUI();
						dismiss();
					}

					@Override
					public void onException(Exception e) {
						dismiss();
					}
				});
	}
}
